<?php

	include 'DefineDB.php';
	$link = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD)
	    or exit(CONNECT_ERR_MESS.' '. mysql_error());
	$db_list = mysql_list_dbs($link);
	$exist_db = false;
	while ($row = mysql_fetch_object($db_list))
	     if($row->Database == MYSQL_DATABASE){
	     	$exist_db = true;
	     	break;
	     }
	if($exist_db){
		mysql_select_db(MYSQL_DATABASE, $link) 
			or die(CONNECT_ERR_MESS);
	}
	else{
		mysql_query('CREATE DATABASE '.MYSQL_DATABASE.';', $link)
			or exit(CONNECT_ERR_MESS.' '.mysql_error());
	}
	$table_list = mysql_list_tables(MYSQL_DATABASE);
	$exist_table = false;
	
	while ($row = mysql_fetch_array($table_list)){
		if($row[0] == MYSQL_TABLE_FEETOTAL){
	     	$exist_table = true;
	     	break;
	     }
	}
	if(!$exist_table){
			$command =
			"\"".MYSQLDIRBIN."mysql.exe\"".
			" --host=".MYSQL_HOST.
			" --user=".MYSQL_USER.
			" --password=".MYSQL_PASSWORD.
			"  ".MYSQL_DATABASE.
			" < \"backup\\maindb.sql\"";
	
			if(system($command) === false)
				echo(CONNECT_ERR_MESS);
	}	

?>